package android.Spider;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.TextView;

public class ThreadReply extends Activity
{

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		
		m_thread.fromBundle(getIntent().getExtras());
		
        m_connection.Initialize(this);

        setContentView(R.layout.threadreply);
		
		TextView tv = (TextView)findViewById(R.id.ThreadReplyTitle);
		tv.setText(m_thread.m_strThreadName);
		
		tv = (TextView)findViewById(R.id.ThreadReplyText);
		tv.setText(m_thread.m_lstPosts.get(0).m_strText);
		
		ProfileUser profile = SpiderPreferences.Instance(this).FindProfile(m_thread.m_lstPosts.get(0).m_strName);
		((ImageView)findViewById(R.id.ThreadReplyAvatarImage)).setImageResource(ProfileUser.idImages[profile.m_nAvatar]);

		((ImageView)findViewById(R.id.ThreadReplyAvatarImageSender)).setImageResource(
					ProfileUser.idImages[SpiderPreferences.Instance(this).GetUserProfile().m_nAvatar]
				);
	}

	@Override
	protected void onDestroy() {
		m_connection.Unregister();
		super.onDestroy();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    super.onCreateOptionsMenu(menu);
	    
	    menu.add(0, 0, "Send", R.drawable.submitsend);
	    menu.add(0, 1, "Discard", R.drawable.discard);
	    return true;
	}

	@Override
	public boolean onOptionsItemSelected(Menu.Item item)
	{
	    if (item.getId() == 0)
	    {
	    	TextView tv = (TextView)findViewById(R.id.ThreadReplyText);
	    	m_connection.SubmitPost(m_thread.m_idThread, tv.getText().toString());
	    	finish();
	    	return true;
	    }
	    else if (item.getId() == 1)
	    {
	    	finish();
	    	return true;
	    }
	    
	    return false;
	}
	
	TopicThread m_thread = new TopicThread();
    XmppConnection m_connection = new XmppConnection() { };
}
